package com.wkg.springblogdemo.common.util;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class Md5Util {


    public static String enrcypt(String content) {
        String salt = UUID.randomUUID().toString().replace("-", "");
        String finalPassword = DigestUtils.md5DigestAsHex((content + salt).getBytes(StandardCharsets.UTF_8));
        return finalPassword + salt;
    }


    public static Boolean verify(String password, String sqlPassword) {
        if (!StringUtils.hasLength(password) || !StringUtils.hasLength(sqlPassword)) {
            return false;
        }
        if (sqlPassword.length() != 64) {
            return false;
        }
        String salt = sqlPassword.substring(32);
        String finalPassword = DigestUtils.md5DigestAsHex((password + salt).getBytes(StandardCharsets.UTF_8));
        return sqlPassword.equals(finalPassword + salt);
    }




}
